"""
网络相关路由 - 支持TCP和UDP
"""
from fastapi import APIRouter, HTTPException
from models import NetConfig, SendDataRequest
from services.net_service import net_manager
from utils.data_utils import format_data_for_send

router = APIRouter(prefix="/api/net", tags=["网络"])

@router.post("/connect")
async def net_connect(config: NetConfig):
    """建立网络连接"""
    print(f"网络连接请求: {config.dict()}")
    
    result = net_manager.create_connection(config)
    
    print(f"网络连接结果: success={result.success}, message={result.message}, data={result.data}")
    
    if not result.success:
        raise HTTPException(status_code=400, detail=result.message)
    return result.dict()

@router.post("/disconnect/{connection_id}")
async def net_disconnect(connection_id: str):
    """断开网络连接"""
    result = net_manager.disconnect(connection_id)
    if not result.success:
        raise HTTPException(status_code=404, detail=result.message)
    return result.dict()

@router.post("/send/{connection_id}")
async def net_send_data(connection_id: str, request: SendDataRequest):
    """发送网络数据"""
    try:
        # 验证请求数据
        if request.data is None:
            raise HTTPException(status_code=400, detail="发送数据不能为None")
        
        # 记录请求信息
        print(f"网络发送数据请求: connection_id={connection_id}, data='{request.data}', format={request.format}")
        
        data = format_data_for_send(request.data, request.format)
        result = net_manager.send_data(connection_id, data)
        
        if not result.success:
            raise HTTPException(status_code=404, detail=result.message)
        
        return result.dict()
        
    except ValueError as e:
        raise HTTPException(status_code=400, detail=f"数据格式错误: {str(e)}")
    except Exception as e:
        print(f"网络发送数据异常: {str(e)}")
        raise HTTPException(status_code=500, detail=f"内部服务器错误: {str(e)}")

@router.get("/status/{connection_id}")
async def net_get_status(connection_id: str):
    """获取网络连接状态"""
    status = net_manager.get_connection_status(connection_id)
    if status is None:
        return {"connected": False}
    return status